package com.kim.whereeat.service;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.kim.whereeat.model.FoodInfo;
import com.kim.whereeat.utils.SQLiteHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 伟阳 on 2015/10/24.
 */
public class FoodInfoService {
    private SQLiteHelper helper;
    private static final String TABLENAME = "foodinfo";

    public FoodInfoService(Context context) {
        helper = new SQLiteHelper(context);
    }

    /**
     * 判断食品是否存在
     *
     * @param foodName 食品名
     * @return true存在/false不存在
     */
    public boolean exists(String foodName) {
        boolean flag = false;
        SQLiteDatabase dbReader = helper.getReadableDatabase();
        Cursor cursor = dbReader.query(TABLENAME, null, "food_name=?", new String[]{foodName}, null, null, null);
        flag = cursor.getColumnCount() > 0;
        cursor.close();
        return flag;
    }

    /**
     * 添加食品
     *
     * @param foodInfo 食品信息
     * @return true添加成功/false添加失败
     */
    public boolean addFood(FoodInfo foodInfo) {
        SQLiteDatabase dbWriter = helper.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("store_id", foodInfo.getStore_id());
        cv.put("food_name", foodInfo.getFood_name());
        cv.put("food_price", foodInfo.getFood_price());
        cv.put("food_introduction", foodInfo.getFood_introduction());
        cv.put("food_image", foodInfo.getFood_image());
        long temp = dbWriter.insert(TABLENAME, null, cv);
        return temp > 0;
    }

    /**
     * 通过ID获取食品信息
     *
     * @param food_id 食品ID
     * @return null/foodInfo
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws NoSuchFieldException
     */
    public FoodInfo getFoodInfoById(int food_id) throws InstantiationException, IllegalAccessException, NoSuchFieldException {
        FoodInfo foodInfo = null;
        SQLiteDatabase dbReader = helper.getReadableDatabase();
        Cursor cursor = dbReader.query(TABLENAME, null, "id=?", new String[]{food_id + ""}, null, null, null);
        while (cursor.moveToNext()) {
            foodInfo = new FoodInfo();
            foodInfo.setId(cursor.getInt(0));
            foodInfo.setFood_name(cursor.getString(1));
            foodInfo.setFood_price(cursor.getDouble(2));
            foodInfo.setFood_image(cursor.getString(3));
            foodInfo.setFood_introduction(cursor.getString(4));
            foodInfo.setStore_id(cursor.getInt(5));
        }
        cursor.close();
        return foodInfo;
    }

    /**
     * 通过商店ID获取商品信息
     *
     * @param store_id 商店ID
     * @return null/foodInfo
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws NoSuchFieldException
     */
    public List<FoodInfo> getFoodInfoByStoreId(int store_id) throws InstantiationException, IllegalAccessException, NoSuchFieldException {
        List<FoodInfo> foodInfos = new ArrayList<>();
        SQLiteDatabase dbReader = helper.getReadableDatabase();
        Cursor cursor = dbReader.query(TABLENAME, null, "store_id=?", new String[]{store_id + ""}, null, null, null);
        while (cursor.moveToNext()) {
            FoodInfo foodInfo = new FoodInfo();
            foodInfo.setId(cursor.getInt(0));
            foodInfo.setFood_name(cursor.getString(1));
            foodInfo.setFood_price(cursor.getDouble(2));
            foodInfo.setFood_image(cursor.getString(3));
            foodInfo.setFood_introduction(cursor.getString(4));
            foodInfo.setStore_id(cursor.getInt(5));
            foodInfos.add(foodInfo);
        }
        cursor.close();
        return foodInfos;
    }

    /**
     * 获取所有
     *
     * @return List<foodInfo>
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws NoSuchFieldException
     */
    public List<FoodInfo> getFoodInfoList() throws InstantiationException, IllegalAccessException, NoSuchFieldException {
        List<FoodInfo> foodInfoList = new ArrayList<>();
        SQLiteDatabase dbReader = helper.getReadableDatabase();
        Cursor cursor = dbReader.query(TABLENAME, null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            FoodInfo foodInfo = new FoodInfo();
            foodInfo.setId(cursor.getInt(0));
            foodInfo.setFood_name(cursor.getString(1));
            foodInfo.setFood_price(cursor.getDouble(2));
            foodInfo.setFood_image(cursor.getString(3));
            foodInfo.setFood_introduction(cursor.getString(4));
            foodInfo.setStore_id(cursor.getInt(5));
            foodInfoList.add(foodInfo);
        }
        cursor.close();
        return foodInfoList;
    }

    /**
     * 通过搜索食品名获取食品信息列表
     *
     * @param food_name 食品名
     * @return List<foodInfo>
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws NoSuchFieldException
     */
    public List<FoodInfo> getfoodInfoListByName(String food_name) throws InstantiationException, IllegalAccessException, NoSuchFieldException {
        List<FoodInfo> foodInfoList = new ArrayList<>();
        SQLiteDatabase dbReader = helper.getReadableDatabase();
        String new_food_name = "%";
        for (int i = 0; i < food_name.length(); i++) {
            new_food_name += food_name.charAt(i) + "%";
        }
        Cursor cursor = dbReader.query(TABLENAME, null, "food_name like ?", new String[]{new_food_name}, null, null, null);
        while (cursor.moveToNext()) {
            FoodInfo foodInfo = new FoodInfo();
            foodInfo.setId(cursor.getInt(0));
            foodInfo.setFood_name(cursor.getString(1));
            foodInfo.setFood_price(cursor.getDouble(2));
            foodInfo.setFood_image(cursor.getString(3));
            foodInfo.setFood_introduction(cursor.getString(4));
            foodInfo.setStore_id(cursor.getInt(5));
            foodInfoList.add(foodInfo);
        }
        cursor.close();
        return foodInfoList;
    }


    /**
     * 关闭数据库
     */
    public void close() {
        if (helper != null)
            helper.close();
    }
}
